from django.shortcuts import render
from telethon import TelegramClient, events

# Create your views here.

api_id = 489553

api_hash = '735d12b12937a3a959591a6d0da7c34b'

client = TelegramClient('stone', api_id, api_hash)

target_chatid = -1001365952172


async def start():
    count = 0
    async for message in client.iter_messages(target_chatid):
        if count < 10:
            print(message.id, message.text, message.photo, message.video)
            await message.download_media('/Users/stone/shidan/static')
            count += 1


@client.on(events.NewMessage)
async def event_handler(event):
    if 'hello' in event.raw_text:
        await event.reply('hi!')



def stop_task(request):
    with client:
        if client.loop.is_running():
            client.loop.stop()


client.start()
client.loop.run_until_complete(start())
client.run_until_disconnected()